/*
 * Email Notify
 * Copyright: (C) 2008 by Antonio Nastasi
 * Email: sifcenter (at) gmail.it
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "editnotification.h"

#include <KGlobal>
#include <KStandardDirs>
#include <KDebug>

EditNotification::EditNotification( KConfigGroup& generalGroup, QWidget* parent )
  : QWidget( parent ),
    media_(0)
{
  setupUi( this );
  generalGroup_ = generalGroup;

  playButton_->setIcon( KIcon( "media-playback-start" ) );

  QStringList allFiles;
  KGlobal::dirs()->findAllResources( "sound", "" , KStandardDirs::Recursive, allFiles );

  soundCombo_->addItem( "(Nothing)" );
  soundCombo_->addItems( allFiles );

  media_ = Phonon::createPlayer( Phonon::MusicCategory );
  Q_ASSERT( media_ != 0 );

  connect( media_,      SIGNAL( finished() ), this, SLOT( finishedSound() ) );
  connect( playButton_, SIGNAL( clicked()  ), this, SLOT( playSound()     ) );

  QStringList clients;
  //TODO add email clients
  clients << "kmail" << "thunderbird" << "firefox" << "konqueror" << "opera";

  foreach( QString client, clients )
  {
    if( KStandardDirs::findExe( client ).isEmpty() )
    {
      clients.removeAll( client );
    }
  }

  emailClientCombo_->addItem( "(Nothing)" );
  emailClientCombo_->addItems( clients );
}



EditNotification::~EditNotification()
{
  kDebug() << "DESTROYED";
  delete media_;
}



void EditNotification::loadConfig()
{
  int i = soundCombo_->findText( generalGroup_.readEntry( "Sound", "(Nothing)" ) );
  soundCombo_->setCurrentIndex( ( i != -1 ) ? i : 0 );

  i = emailClientCombo_->findText( generalGroup_.readEntry( "EmailClient", "(Nothing)" ) );
  emailClientCombo_->setCurrentIndex( ( i != -1 ) ? i : 0 );
  //balloonCheck_->setChecked( generalGroup_->readEntry( "Balloon", bool );
}



void EditNotification::saveConfig()
{
  QString sound_file( soundCombo_->itemText( soundCombo_->currentIndex() ) );

  if ( sound_file.isEmpty() )
  {
    generalGroup_.writeEntry( "Sound", "(Nothing)" );
  }
  else
  {
    generalGroup_.writeEntry( "Sound", sound_file );
  }
 // generalGroup_->writeEntry( "Balloon", balloonCheck_->isChecked() );
  generalGroup_.writeEntry( "EmailClient", emailClientCombo_->currentText() );
}



void EditNotification::playSound()
{
  if( media_ == 0 )
  {
    return;
  }

  QString current( soundCombo_->itemText( soundCombo_->currentIndex() ) );
  if( ( current == "(Nothing)" ) || current.isEmpty() )
  {
    return;
  }

  QString file( KGlobal::dirs()->locate( "sound", current) );
  if( file.isEmpty() )
  {
    return;
  }

  media_->setCurrentSource( file );
  media_->play();
  playButton_->setIcon( KIcon( "media-playback-stop" ) );
}



void EditNotification::finishedSound()
{
  playButton_->setIcon( KIcon( "media-playback-start" ) );
}

#include "editnotification.moc"
